package main

import (
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	global2 "longmen/server/config/global"
	_ "longmen/server/docs"
	"longmen/server/internal/api"
	"longmen/server/internal/router"
	"longmen/server/pkg/common/helper/encrypt"
	middlewares2 "longmen/server/pkg/common/middlewares"
	"longmen/server/pkg/common/services"
	"longmen/server/pkg/db/redis/cache"
	"longmen/server/pkg/grpc_client"
)

func main() {
	global2.Initialize("../../../config/app.properties")
	global2.ReloadAutoLive("../../../config/auto_live_config.json")
	e := echo.New()
	e.Debug = true
	service := &services.Service{global2.GetX()}
	service.InitLiveCallBackServer()
	base := api.Controller{
		Service: service,
	}
	reqBodyFunc := middlewares2.ReqBodyWithConfig(middlewares2.ReqBodyConfig{})
	authFunc := middlewares2.BasicAuthWithConfig(middlewares2.BasicAuthConfig{
		BasicAuthSkipper: middleware.DefaultSkipper,
		Service:          service,
	})
	signConfig := middlewares2.SignatureConfig(middlewares2.SignatureAuthConfig{})
	//p := prometheus.NewPrometheus("echo", nil)
	//p.Use(e)
	middlewares2.InitSkipVerifyFunc()
	e.Use(
		middlewares2.CheckMaintain(), reqBodyFunc, signConfig, middleware.CORSWithConfig(middleware.DefaultCORSConfig), middleware.RecoverWithConfig(middleware.RecoverConfig{
			StackSize: 1 << 10, //1kb
		}),
		middlewares2.Limiter,
		middlewares2.GlobalIP,
	)

	router.InitRouter(e, base, authFunc)
	err := global2.InitElasticSearch()
	if err != nil {
		global2.Log.Fatalf("%s", err.Error())
	}
	global2.InitPublisher()
	global2.InitMQ()
	grpc_client.InitGrpc()
	grpc_client.InitTimGrpc()
	cache.SetAESKey()
	encrypt.InitBaseMap()
	//pyroscope.InitPyroScope("go_api")

	e.Logger.Fatal(e.Start(":" + global2.Config.GetString("sys.port")))

}
